package global

import (
	"gopkg.in/yaml.v3"
	"io/ioutil"
	"path/filepath"
	"time"
)

func InitSiteConf() (err error) {
	err = initSiteConf(&SiteConf)
	return
}

func initSiteConf(c *SiteConfig) (err error) {
	// get the abs
	// which will try to find the 'filename' from current workind dir too.
	yamlAbsPath, err := filepath.Abs("./conf.yml")
	if err != nil {
		return
	}

	// read the raw contents of the file
	data, err := ioutil.ReadFile(yamlAbsPath)
	if err != nil {
		return
	}

	// put the file's contents as yaml to the default configuration(c)
	err = yaml.Unmarshal(data, &c)
	return
}

// 站点信息配置
type SiteConfig struct {
	SiteName string      `yaml:"SiteName,omitempty"` // 站点名称
	Host     string      `yaml:"Host,omitempty"`     //
	WebUrl   string      `yaml:"WebUrl,omitempty"`   // 网站url
	ResUrl   string      `yaml:"ResUrl,omitempty"`   // 静态资源url
	IsProd   bool        `yaml:"IsProd,omitempty"`   //
	PProf    bool        `yaml:"Pprof,omitempty"`    // 是否开启pprof, 开启后访问 /debug/pprof
	Mongo    MongoConfig `yaml:"MongoDB,omitempty"`  // MongoDB 配置
	Redis    RedisConfig `yaml:"Redis,omitempty"`    // Redis 配置
}

// MongoDB 配置
type MongoConfig struct {
	Host     []string `yaml:"host,omitempty"`
	Database string   `yaml:"database,omitempty"`
	User     string   `yaml:"user,omitempty"`
	Password string   `yaml:"password,omitempty"`
	Timeout  int      `yaml:"timeout,omitempty"`
}

// Redis 配置
type RedisConfig struct {
	Host      string `yaml:"host,omitempty"`
	Port      string `yaml:"port,omitempty"`
	Database  int    `yaml:"database,omitempty"`
	Password  string `yaml:"password,omitempty"`
	MaxIdle   int    `yaml:"maxIdle,omitempty"`
	MaxActive int    `yaml:"maxActive,omitempty"`
}

// jwt 配置
type JwtConfig struct {
	Secret string        `yaml:"secret,omitempty"`
	TTL    time.Duration `yaml:"ttl,omitempty"`
}
